/**

 @Name：后台权限_权限
 @Author：gz
 @Site：http://www.layui.com/admin/
 @License: LPPL
    
 */
 
layui.define(['table', 'form'], function(exports){
  var $ = layui.$
  ,layer = layui.layer
  ,laytpl = layui.laytpl
  ,setter = layui.setter
  ,admin = layui.admin
  ,view = layui.view
  ,table = layui.table
  ,router = layui.router()
  ,form = layui.form;

  //管理员列表
  table.render({
    elem: '#admin_user_list'
    ,url: '/index/Role/lists' //接口
    ,page: true
    ,cols: [[
      {type: 'checkbox', fixed: 'left'}
      ,{field: 'id', width: 80, title: 'ID', sort: true}
      ,{field: 'title', title: '名称'}
      ,{field: 'status', title:'状态', templet: '#buttonTpl', minWidth: 80, align: 'center'}
      ,{title: '操作', width: 150, align: 'center', fixed: 'right', toolbar: '#table-useradmin-admin'}
    ]]
    ,text: '对不起，加载出现异常！'
  });

  //监听工具条
  table.on('tool(admin_user_list)', function(obj){
    var data = obj.data;

    if(obj.event === 'del'){
      admin.req({
          url: '/index/common/check_node?url=/Role/del'//权限接口
          ,done: function(res){
              layer.prompt({
                formType: 1
                ,title: '敏感操作，请验证超级管理员口令'
              }, function(value, index){
                  //验证口令
                  //console.log(value);
                  admin.req({
                    url: '/index/Role/check_admin' //验证超级管理员密码接口
                    ,type: 'post'
                    ,data: {password:value}
                    ,done: function(res){
                      layer.confirm('确定删除此管理组？', function(index){
                        //console.log(obj.data)
                        //ajax 提交删除信息
                        admin.req({
                          url: '/index/Role/del' //管理员删除接口
                          ,type: 'post'
                          ,data: {id:obj.data.id}
                          ,done: function(res){
                            obj.del();
                            layer.close(index);
                          }
                        });
                        //
                      });

                    }
                  });
                  layer.close(index);
              });

          }
      });

    }else if(obj.event === 'edit'){
      admin.req({
        url: '/index/common/check_node?url=/Role/edit'
        ,done: function(res){

          admin.popup({
            title: '编辑管理员'
            ,area: ['420px', '450px']
            ,id: 'LAY-popup-user-add'
            ,success: function(layero, index){
              //console.log(data);数据信息
              view(this.id).render('Jurisdiction/group/adminform', data).done(function(){
                form.render(null, 'layuiadmin-form-admin');
                //监听提交
                form.on('submit(LAY-user-back-submit)', function(data){
                  var field = data.field; //获取提交的字段
                  //提交 Ajax 成功后，关闭当前弹层并重载表格
                  var rules='';
                  $("input:checkbox[name='layTableCheckbox']:checked").each(function() { // 遍历name=layTableCheckbox的多选框
                    if ($(this).val()!='on') {
                      rules += $(this).val()+',';
                    }
                      
                  });
                  admin.req({
                    url: '/index/Role/edit' //实际使用请改成服务端真实接口
                    ,type: 'post'
                   ,data: {data:field,rules:rules}
                    ,done: function(res){
                      layui.table.reload('admin_user_list'); //重载表格
                      layer.close(index); //执行关闭 
                    }
                  });

                  
                });
              });
            }
          });
        }
      });
    }
  });


  //监听搜索
  form.on('submit(admin_user_list_search)', function(data){
    var field = data.field;
    
    //执行重载
    table.reload('admin_user_list', {
      where: field
    });
  });

  //事件
  var active = {
    add: function(){
      admin.req({
      url: '/index/common/check_node?url=/Role/add'
      ,done: function(res){
        if (res.code=='1002') {
          layer.msg(res.msg, {icon: 2});
        }else{
          //请求成功后
          admin.popup({
            title: '添加管理员'
            ,area: ['420px', '450px']
            ,id: 'LAY-popup-useradmin-add'
            ,success: function(layero, index){
              view(this.id).render('Jurisdiction/group/adminform').done(function(){
                form.render(null, 'layuiadmin-form-admin');
                //监听提交
                form.on('submit(LAY-user-back-submit)', function(data){
                  var field = data.field; //获取提交的字段
                  //提交 Ajax 成功后，关闭当前弹层并重载表格
                  var rules='';
                  $("input:checkbox[name='layTableCheckbox']:checked").each(function() { // 遍历name=layTableCheckbox的多选框
                    if ($(this).val()!='on') {
                      rules += $(this).val()+',';
                    }
                      
                  });
                  admin.req({
                  url: '/index/Role/add' //实际使用请改成服务端真实接口
                  ,type: 'post'
                  ,data: {data:field,rules:rules}
                  ,done: function(res){
                    layui.table.reload('admin_user_list'); //重载表格
                    layer.close(index); //执行关闭 
                  }
                  });
                  
                });
              });
            }
          });
        }










      }
    });

    }
  }  
  $('.layui-btn.layuiadmin-btn-admin').on('click', function(){
    var type = $(this).data('type');
    active[type] ? active[type].call(this) : '';
  });





  //对外暴露的接口
  exports('jurisdiction_group', {});
});